package com.spark.mooc.ch2_scala.part02_objectOrientedProgramming

/**
 * @description:
 * @time: 2020/11/24 8:25
 * @author: lhy
 */
class Counter {
    private var privateValue = 0;
    def value = privateValue
    def value_=(newValue:Int): Unit ={
        if (newValue > 0)
            privateValue = newValue
    }
    def increment(step:Int) = {
        value += step
    }
    def current():Int = {value}
}
class Counter2{
    var value = 0
    def increment(step:Int)= value += step
    def current:Int = value
    def getValue():Int = value
}
//主构造器
class Counter3(var name:String)
//辅构造器
class Counter4{
    private var value = 0
    private var name = ""
    private var step = 1
    println("the main constructor")
    def this(name:String){  //第一个辅助构造器
        this() //调用主构造器
        this.name = name
        printf("the first auxiliary constructor,name:%s\n",name)
    }
    def this(name:String,step:Int){     //第二个辅助构造器
        this(name)  //调用前一个构造器
        this.step = step
        printf("the second auxiliary constructor,name:%s,step:%d\n",name,step)
    }
    def increment(step:Int)= value += step
    def current:Int = value
}
object Test{
    def main(args: Array[String]): Unit = {
        val myCounter = new Counter
        myCounter.value_= (3)
        println(myCounter.value)

        val c3 = new Counter3("Runner")
        println(c3.name)
        c3.name_= ("Timer")
        println(c3.name)
        c3.name = "good"
        println(c3.name)

        val c4 = new Counter4()
        val c4_1 = new Counter4("the 2nd Counter")
        val c4_2 = new Counter4("the 3nd Counter",2)

    }
}

